<!DOCTYPE html>
<html lang="zh" data-direction="ltr">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta
      name="keywords"
      content="Go-Micro, Micro, 中文, Go-Micro教程, Go-Micro资源"
    />
    <meta
      name="description"
      content="Micro 中国站，只为原生中文资源"
    />
    <title>Go Micro - Micro 中国</title>
    <meta data-rh="true" property="og:type" content="website"/><meta data-rh="true" property="og:image" content=""/><meta data-rh="true" property="og:title" content="Go Micro - Micro 中国"/><meta data-rh="true" name="description" content="Go Micro 是可插拔的微服务开发框架。"/>
    <link
      rel="icon"
      href="https://avatars2.githubusercontent.com/u/20906580?s=60&v=4"
      type="image/x-icon"
    />
    
    <link rel="stylesheet" type="text/css" href="/index-47dfeecd.css" />
    
    <style id="nprogress-style">
      #nprogress {
        display: none;
      }
    </style>
    <link rel="stylesheet/less" type="text/css" href="/color.less" />
    <script src="https://polyfill.alicdn.com/polyfill.min.js?features=default,es2015,Intl"></script>
    <script>
      (function () {
        function isLocalStorageNameSupported() {
          var testKey = 'test';
          var storage = window.localStorage;
          try {
            storage.setItem(testKey, '1');
            storage.removeItem(testKey);
            return true;
          } catch (error) {
            return false;
          }
        }
        // 优先级提高到所有静态资源的前面，语言不对，加载其他静态资源没意义
        var pathname = location.pathname;

        function isZhCN(pathname) {
          return /-cn\/?$/.test(pathname);
        }
        function getLocalizedPathname(path, zhCN) {
          var pathname = path.startsWith('/') ? path : '/' + path;
          if (!zhCN) {
            // to enUS
            return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
          } else if (pathname === '/') {
            return '/index-cn';
          } else if (pathname.endsWith('/')) {
            return pathname.replace(/\/$/, '-cn/');
          }
          return pathname + '-cn';
        }

        // 兼容旧的 URL， `?locale=...`
        var queryString = location.search;
        if (queryString) {
          var isZhCNConfig = queryString.indexOf('zh-CN') > -1;
          if (isZhCNConfig && !isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, isZhCNConfig);
          }
        }

        // 首页无视链接里面的语言设置 https://github.com/ant-design/ant-design/issues/4552
        if (isLocalStorageNameSupported() && (pathname === '/' || pathname === '/index-cn')) {
          var lang =
            (window.localStorage && localStorage.getItem('locale')) ||
            ((navigator.language || navigator.browserLanguage).toLowerCase() === 'zh-cn'
              ? 'zh-CN'
              : 'en-US');
          // safari is 'zh-cn', while other browser is 'zh-CN';
          if ((lang === 'zh-CN') !== isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, lang === 'zh-CN');
          }
        }
        document.documentElement.className += isZhCN(pathname) ? 'zh-cn' : 'en-us';
      })();
    </script>
  </head>
  <body>
    <div id="react-content">
      <div class="page-wrapper"><header id="header" class="clearfix"><div class="ant-row" style="flex-flow:nowrap;height:64px"><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><h1><a id="logo" href="/index-cn"><img alt="logo" src=""/>Micro 中国</a></h1></div><div class="ant-col menu-row ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><div id="search-box" class=""><span role="img" aria-label="search" class="anticon anticon-search"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span><input type="text" placeholder="在 Micro China 中搜索" class="ant-input" value=""/></div><ul class="ant-menu menu-site ant-menu-light ant-menu-root ant-menu-horizontal" id="nav" direction="ltr" role="menu"><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/micro/introduce-cn">文档</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/components/button-cn/">插件</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/resources-cn">资源</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul><button type="button" class="ant-btn header-button header-lang-button ant-btn-sm">繁体中文</button><button type="button" class="ant-btn header-button ant-dropdown-trigger ant-btn-sm">更多<span role="img" aria-label="down" style="font-size:9px;margin-left:2px;vertical-align:middle;margin-top:-1px" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></button><span id="github-btn" class="github-btn"><a class="gh-btn" href="//github.com/micro/go-micro/" target="_blank"><span class="gh-ico" aria-hidden="true"></span><span class="gh-text">Star</span></a><a class="gh-count" target="_blank" href="//github.com/micro/go-micro/stargazers/"></a></span></div></div></header><div class="main-wrapper"><div class="ant-row"><div class="ant-col main-menu ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><div><div class=""><section class="main-menu-inner"><ul class="ant-menu aside-container menu-site ant-menu-light ant-menu-root ant-menu-inline" direction="ltr" role="menu"><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/getting-starter-cn">快速上手</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/api-cn">API</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/architecture-cn">架构</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/bot-cn">机器人</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/cli-cn">CLI</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/debugging-cn">Debug</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/deploy-docker-cn">使用Docker发布</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/faq-cn">常见问题</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/deploy-kubernetes-cn">kubernetes</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/fault-tolerance-cn">容错</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/features-cn">特性</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-api-cn">Go API</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-config-cn">Go Config 配置库</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-grpc-cn">Go gRPC支持</a></li><li class="ant-menu-item ant-menu-item-only-child ant-menu-item-selected" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-micro-cn">Go Micro</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-micro-internals-cn">Go Micro 架构</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/grpc-gateway-cn">GRPC 网关</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro-cn">Micro 文档</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/install-guide-cn">安装指导</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/writing-a-go-function-cn">编写Golang Function</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/writing-a-go-service-cn">编写Golang服务</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/introduce-cn">Go-Micro 框架</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/plugins-cn">插件库</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/plugins-go-micro-cn">Go Micro插件</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/proxy-cn">Proxy</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/runtime-cn">Micro Runtime</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/resources-cn">相关资源</a></li></ul></section></div></div></div><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><section class="main-container"><article class="markdown"><div><h1>Go Micro<a class="edit-button" href="https://github.com/ant-design/ant-design/edit/master/docs/micro/go-micro.zh-CN.md" target="_blank" rel="noopener noreferrer"><span role="img" aria-label="edit" class="anticon anticon-edit"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="edit" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M257.7 752c2 0 4-.2 6-.5L431.9 722c2-.4 3.9-1.3 5.3-2.8l423.9-423.9a9.96 9.96 0 000-14.1L694.9 114.9c-1.9-1.9-4.4-2.9-7.1-2.9s-5.2 1-7.1 2.9L256.8 538.8c-1.5 1.5-2.4 3.3-2.8 5.3l-29.5 168.2a33.5 33.5 0 009.4 29.8c6.6 6.4 14.9 9.9 23.8 9.9zm67.4-174.4L687.8 215l73.3 73.3-362.7 362.6-88.9 15.7 15.6-89zM880 836H144c-17.7 0-32 14.3-32 32v36c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-36c0-17.7-14.3-32-32-32z"></path></svg></span></a></h1></div><div class="toc-affix"><div class=""><ul class="toc"><li><a class="bisheng-toc-h2" href="#概览" title="概览">概览</a></li><li><a class="bisheng-toc-h2" href="#特性" title="特性">特性</a></li><li><a class="bisheng-toc-h2" href="#安装-protobuf" title="安装 protobuf">安装 protobuf</a></li><li><a class="bisheng-toc-h2" href="#服务发现" title="服务发现">服务发现</a></li><li><a class="bisheng-toc-h2" href="#编写服务" title="编写服务">编写服务</a></li><li><a class="bisheng-toc-h2" href="#编写-Function" title="编写 Function">编写 Function</a></li><li><a class="bisheng-toc-h2" href="#发布与订阅" title="发布与订阅">发布与订阅</a></li><li><a class="bisheng-toc-h2" href="#插件" title="插件">插件</a></li><li><a class="bisheng-toc-h2" href="#包装器（Wrappers）" title="包装器（Wrappers）">包装器（Wrappers）</a></li><li><a class="bisheng-toc-h2" href="#相关示例" title="相关示例">相关示例</a></li></ul></div></div><section class="markdown"><p>Go Micro 是可插拔的微服务开发框架。</p><h2 id="概览"><span>概览</span><a href="#概览" class="anchor">#</a></h2><p>Go Micro 提供分布式系统开发的核心库，包含 RPC 与事件驱动的通信机制。</p><p><strong>micro</strong>的设计哲学是可插拔的架构理念，她提供可快速构建系统的组件，并且可以根据自身的需求剥离默认实现并自行定制。</p><h2 id="特性"><span>特性</span><a href="#特性" class="anchor">#</a></h2><p>Go Micro 把分布式系统的各种细节抽象出来。下面是它的主要特性。</p><ul><li><p><strong>服务发现（Service Discovery）</strong> - 自动服务注册与名称解析。服务发现是微服务开发中的核心。当服务 A 要与服务 B 协作时，它得知道 B 在哪里。默认的服务发现系统是 Consul，而 multicast DNS (mdns，组播)机制作为本地解决方案，或者零依赖的 P2P 网络中的 SWIM 协议（gossip）。</p></li><li><p><strong>负载均衡（Load Balancing）</strong> - 在服务发现之上构建了负载均衡机制。当我们得到一个服务的任意多个的实例节点时，我们要一个机制去决定要路由到哪一个节点。我们使用随机处理过的哈希负载均衡机制来保证对服务请求颁发的均匀分布，并且在发生问题时进行重试。</p></li><li><p><strong>消息编码（Message Encoding）</strong> - 支持基于内容类型（content-type）动态编码消息。客户端和服务端会一起使用 content-type 的格式来对 Go 进行无缝编/解码。各种各样的消息被编码会发送到不同的客户端，客户端服服务端默认会处理这些消息。content-type 默认包含 proto-rpc 和 json-rpc。</p></li><li><p><strong>Request/Response</strong> - RPC 通信基于支持双向流的请求/响应方式，我们提供有抽象的同步通信机制。请求发送到服务时，会自动解析、负载均衡、拨号、转成字节流，默认的传输协议是 http/1.1，而 tls 下使用 http2 协议。</p></li><li><p><strong>异步消息（Async Messaging）</strong> - 发布订阅（PubSub）头等功能内置在异步通信与事件驱动架构中。事件通知在微服务开发中处于核心位置。默认的消息传送使用点到点 http/1.1，激活 tls 时则使用 http2。</p></li><li><p><strong>可插拔接口（Pluggable Interfaces）</strong> - Go Micro 为每个分布式系统抽象出接口。因此，Go Micro 的接口都是可插拔的，允许其在运行时不可知的情况下仍可支持。所以只要实现接口，可以在内部使用任何的技术。更多插件请参考：<a href="https://github.com/micro/go-plugins">github.com/micro/go-plugins</a>。</p></li></ul><h2 id="安装-protobuf"><span>安装 protobuf</span><a href="#安装-protobuf" class="anchor">#</a></h2><p>代码生成依赖 Protobuf，参考安装：</p><ul><li><p><a href="https://github.com/micro/protoc-gen-micro">protoc-gen-micro</a></p></li></ul><h2 id="服务发现"><span>服务发现</span><a href="#服务发现" class="anchor">#</a></h2><p>服务发现用于解析服务名与地址。</p><h3 id="Consul"><span>Consul</span><a href="#Consul" class="anchor">#</a></h3><p><a href="https://www.consul.io/">Consul</a> 在 Go Micro 中是默认（以后的版本可能会更改）的服务发现注册中心。</p><p>发现系统可插拔，其它插件像 etcd, kubernetes, zookeeper 可以参考<a href="https://github.com/micro/go-plugins">micro/go-plugins</a> 库。</p><p><a href="https://www.consul.io/intro/getting-started/install.html">Consul 安装指导</a></p><p>安装好后，可以在命令行启动时传入变量或指令便可以使用 consul 作为注册中心：</p><pre class="language-bash"><code>$ MICRO_REGISTRY<span class="token operator">=</span>consul go run main.go</code></pre><p>或者</p><pre class="language-bash"><code>$ go run main.go --registry<span class="token operator">=</span>consul</code></pre><h3 id="Multicast-DNS"><span>Multicast DNS</span><a href="#Multicast-DNS" class="anchor">#</a></h3><p><a href="https://en.wikipedia.org/wiki/Multicast_DNS">组播 dns</a>内置在服务发现系统之中的插件，提供零依赖的配置。</p><p>在启动命令中传入 <code>--registry=mdns</code>，或声明环境变量 <code>MICRO_REGISTRY=mdns</code>：</p><pre class="language-null"><code>MICRO_REGISTRY<span class="token operator">=</span>mdns go run main<span class="token punctuation">.</span>go</code></pre><h2 id="编写服务"><span>编写服务</span><a href="#编写服务" class="anchor">#</a></h2><p>我们写一个简单的问候（greeter）程序作为示例。</p><p>例子可参考：<a href="https://github.com/micro/examples/tree/master/service">examples/service</a>.</p><h3 id="服务原型"><span>服务原型</span><a href="#服务原型" class="anchor">#</a></h3><p>微服务中有个关键需求点，就是接口的强定义。Micro 使用 protobuf 来完成这个需求。下面我们定义 Greeter 处理器，它有一个 Hello 方法。它有 HelloRequest 入参对象及 HelloResponse 出参对象，两个对象都有一个字符串类型的参数。</p><pre class="language-proto"><code>syntax <span class="token operator">=</span> <span class="token string">"proto3"</span><span class="token comment" spellcheck="true">;</span>

service Greeter {
	rpc <span class="token function">Hello</span><span class="token punctuation">(</span>HelloRequest<span class="token punctuation">)</span> returns <span class="token punctuation">(</span>HelloResponse<span class="token punctuation">)</span> {}
}

message HelloRequest {
	string name <span class="token operator">=</span> <span class="token number">1</span><span class="token comment" spellcheck="true">;</span>
}

message HelloResponse {
	string greeting <span class="token operator">=</span> <span class="token number">2</span><span class="token comment" spellcheck="true">;</span>
}</code></pre><h3 id="生成原型"><span>生成原型</span><a href="#生成原型" class="anchor">#</a></h3><p>在定义好原型后我们得使用 protoc 及 micro 的插件编译它，micro 插件可以帮助生成 go micro 需要的原型文件</p><pre class="language-shell"><code>protoc <span class="token operator">-</span><span class="token operator">-</span>proto_path<span class="token operator">=</span><span class="token variable">$GOPATH</span><span class="token operator">/</span>src<span class="token punctuation">:</span><span class="token punctuation">.</span> <span class="token operator">-</span><span class="token operator">-</span>micro_out<span class="token operator">=</span><span class="token punctuation">.</span> <span class="token operator">-</span><span class="token operator">-</span>go_out<span class="token operator">=</span><span class="token punctuation">.</span> path<span class="token operator">/</span><span class="token keyword">to</span><span class="token operator">/</span>greeter<span class="token punctuation">.</span>proto</code></pre><h3 id="编写服务"><span>编写服务</span><a href="#编写服务" class="anchor">#</a></h3><p>下方的代码是 greeter 服务的代码</p><p>它要实现下面的几个要求</p><ol><li><p>实现在 Greeter Handler 中定义的接口。</p></li><li><p>初始化 micro.Service</p></li><li><p>注册 Greeter handler</p></li><li><p>运行服务</p></li></ol><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">"context"</span>
	<span class="token string">"fmt"</span>

	micro <span class="token string">"github.com/micro/go-micro/v2"</span>
	proto <span class="token string">"github.com/micro/examples/service/proto"</span>
<span class="token punctuation">)</span>

<span class="token keyword">type</span> Greeter <span class="token keyword">struct</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>g <span class="token operator">*</span>Greeter<span class="token punctuation">)</span> <span class="token function">Hello</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelloRequest<span class="token punctuation">,</span> rsp <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelloResponse<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	rsp<span class="token punctuation">.</span>Greeting <span class="token operator">=</span> <span class="token string">"Hello "</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>Name
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment" spellcheck="true">// 创建新的服务，这里可以传入其它选项。</span>
	service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>
		micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 初始化方法会解析命令行标识</span>
	service<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 注册处理器</span>
	proto<span class="token punctuation">.</span><span class="token function">RegisterGreeterHandler</span><span class="token punctuation">(</span>service<span class="token punctuation">.</span><span class="token function">Server</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">new</span><span class="token punctuation">(</span>Greeter<span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 运行服务</span>
	<span class="token keyword">if</span> err <span class="token operator">:=</span> service<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><h3 id="运行服务"><span>运行服务</span><a href="#运行服务" class="anchor">#</a></h3><pre class="language-null"><code>go run examples<span class="token operator">/</span>service<span class="token operator">/</span>main<span class="token punctuation">.</span>go</code></pre><p>输出</p><pre class="language-null"><code><span class="token number">2016</span><span class="token operator">/</span><span class="token number">03</span><span class="token operator">/</span><span class="token number">14</span> <span class="token number">10</span><span class="token punctuation">:</span><span class="token number">59</span><span class="token punctuation">:</span><span class="token number">14</span> Listening on <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span><span class="token number">50137</span>
<span class="token number">2016</span><span class="token operator">/</span><span class="token number">03</span><span class="token operator">/</span><span class="token number">14</span> <span class="token number">10</span><span class="token punctuation">:</span><span class="token number">59</span><span class="token punctuation">:</span><span class="token number">14</span> Broker Listening on <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span><span class="token number">50138</span>
<span class="token number">2016</span><span class="token operator">/</span><span class="token number">03</span><span class="token operator">/</span><span class="token number">14</span> <span class="token number">10</span><span class="token punctuation">:</span><span class="token number">59</span><span class="token punctuation">:</span><span class="token number">14</span> Registering node<span class="token punctuation">:</span> greeter<span class="token operator">-</span>ca62b017<span class="token operator">-</span>e9d3<span class="token operator">-</span><span class="token number">11e5</span><span class="token operator">-</span>9bbb<span class="token operator">-</span>68a86d0d36b6</code></pre><h3 id="定义客户端"><span>定义客户端</span><a href="#定义客户端" class="anchor">#</a></h3><p>下面的客户端代码用来查询 greeter 服务。上面我们生成的 proto 原型文件中包含了客户端部分，这样可以减少模板代码量。</p><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">"context"</span>
	<span class="token string">"fmt"</span>

	micro <span class="token string">"github.com/micro/go-micro/v2"</span>
	proto <span class="token string">"github.com/micro/examples/service/proto"</span>
<span class="token punctuation">)</span>


<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment" spellcheck="true">// 定义服务，可以传入其它可选参数</span>
	service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter.client"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	service<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 创建新的客户端</span>
	greeter <span class="token operator">:=</span> proto<span class="token punctuation">.</span><span class="token function">NewGreeterService</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">,</span> service<span class="token punctuation">.</span><span class="token function">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 调用greeter</span>
	rsp<span class="token punctuation">,</span> err <span class="token operator">:=</span> greeter<span class="token punctuation">.</span><span class="token function">Hello</span><span class="token punctuation">(</span>context<span class="token punctuation">.</span><span class="token function">TODO</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>proto<span class="token punctuation">.</span>HelloRequest<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">"John"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment" spellcheck="true">// 打印响应请求</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>rsp<span class="token punctuation">.</span>Greeting<span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><h3 id="运行客户端"><span>运行客户端</span><a href="#运行客户端" class="anchor">#</a></h3><pre class="language-shell"><code>go run client<span class="token punctuation">.</span>go</code></pre><p>输出</p><pre class="language-null"><code>Hello John</code></pre><h2 id="编写-Function"><span>编写 Function</span><a href="#编写-Function" class="anchor">#</a></h2><p>Go Micro 包含了函数式编程模型。</p><p>Function 是指接收一次请求，执行后便退出的服务</p><h3 id="定义-Function"><span>定义 Function</span><a href="#定义-Function" class="anchor">#</a></h3><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">"context"</span>

	proto <span class="token string">"github.com/micro/examples/function/proto"</span>
	<span class="token string">"github.com/micro/go-micro/v2"</span>
<span class="token punctuation">)</span>

<span class="token keyword">type</span> Greeter <span class="token keyword">struct</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>g <span class="token operator">*</span>Greeter<span class="token punctuation">)</span> <span class="token function">Hello</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelloRequest<span class="token punctuation">,</span> rsp <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelloResponse<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	rsp<span class="token punctuation">.</span>Greeting <span class="token operator">=</span> <span class="token string">"Hello "</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>Name
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment" spellcheck="true">// 创建新函数</span>
	fnc <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewFunction</span><span class="token punctuation">(</span>
		micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 初始化命令行</span>
	fnc<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 注册handler</span>
	fnc<span class="token punctuation">.</span><span class="token function">Handle</span><span class="token punctuation">(</span><span class="token function">new</span><span class="token punctuation">(</span>Greeter<span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment" spellcheck="true">// 运行服务</span>
	fnc<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><p>简单吧！</p><h2 id="发布与订阅"><span>发布与订阅</span><a href="#发布与订阅" class="anchor">#</a></h2><p>Go-micro 给事件驱动架构内置了消息代理（broker）接口。发布与订阅像 RPC 一样操控生成的 protobuf 消息。这些消息会自动编/解码并通过代理发送。</p><p>Go-micro 默认包含点到点的 http 代理，但是也可以通过 go-plugins 把这层逻辑替换掉。</p><h3 id="发布"><span>发布</span><a href="#发布" class="anchor">#</a></h3><p>创建发布器，传入<code>topic</code>主题名，及服务客户端。</p><pre class="language-go"><code>p <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewPublisher</span><span class="token punctuation">(</span><span class="token string">"events"</span><span class="token punctuation">,</span> service<span class="token punctuation">.</span><span class="token function">Client</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></code></pre><p>发布一条 protobuf 消息</p><pre class="language-go"><code>p<span class="token punctuation">.</span><span class="token function">Publish</span><span class="token punctuation">(</span>context<span class="token punctuation">.</span><span class="token function">TODO</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>proto<span class="token punctuation">.</span>Event<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">"event"</span><span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre><h3 id="订阅"><span>订阅</span><a href="#订阅" class="anchor">#</a></h3><p>创建消息处理器，签名得是<code>func(context.Context, v interface{}) error</code>。</p><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">ProcessEvent</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> event <span class="token operator">*</span>proto<span class="token punctuation">.</span>Event<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"Got event %+v\n"</span><span class="token punctuation">,</span> event<span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span></code></pre><p>在这个消息处理器注册上<code>topic</code>主题</p><pre class="language-go"><code>micro<span class="token punctuation">.</span><span class="token function">RegisterSubscriber</span><span class="token punctuation">(</span><span class="token string">"events"</span><span class="token punctuation">,</span> ProcessEvent<span class="token punctuation">)</span></code></pre><p>查看完成例子：<a href="https://github.com/micro/examples/tree/master/pubsub">examples/pubsub</a></p><h2 id="插件"><span>插件</span><a href="#插件" class="anchor">#</a></h2><p>Go-micro 默认下只提供了少量的核心接口实现，但是这些都是可插拔的。<a href="https://github.com/micro/go-plugins">github.com/micro/go-plugins</a>提供了一捆插件，可以供参考，也欢迎贡献您的代码。</p><h3 id="构建插件"><span>构建插件</span><a href="#构建插件" class="anchor">#</a></h3><p>如果想要集成插件，只需要把插件位置导入到文件中，重新编译即可。</p><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
        <span class="token comment" spellcheck="true">// etcd v3 registry</span>
        <span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/registry/etcdv3"</span>
        <span class="token comment" spellcheck="true">// nats transport</span>
        <span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/transport/nats"</span>
        <span class="token comment" spellcheck="true">// kafka broker</span>
        <span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/broker/kafka"</span>
<span class="token punctuation">)</span></code></pre><p>打包二进制文件：</p><pre class="language-shell"><code><span class="token operator">/</span><span class="token operator">/</span> 本地使用
go build <span class="token operator">-</span>i <span class="token operator">-</span>o service <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go <span class="token punctuation">.</span><span class="token operator">/</span>plugins<span class="token punctuation">.</span>go</code></pre><p>标识使用插件</p><pre class="language-shell"><code>service <span class="token operator">-</span><span class="token operator">-</span>registry<span class="token operator">=</span>etcdv3 <span class="token operator">-</span><span class="token operator">-</span>transport<span class="token operator">=</span>nats <span class="token operator">-</span><span class="token operator">-</span>broker<span class="token operator">=</span>kafka</code></pre><h3 id="插件即选项"><span>插件即选项</span><a href="#插件即选项" class="anchor">#</a></h3><p>另外，你也可以在服务中设置插件作为选项</p><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
        <span class="token string">"github.com/micro/go-micro/v2"</span>
        <span class="token comment" spellcheck="true">// etcd v3 registry</span>
        <span class="token string">"github.com/micro/go-plugins/registry/etcdv3"</span>
        <span class="token comment" spellcheck="true">// nats transport</span>
        <span class="token string">"github.com/micro/go-plugins/transport/nats"</span>
        <span class="token comment" spellcheck="true">// kafka broker</span>
        <span class="token string">"github.com/micro/go-plugins/broker/kafka"</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	registry <span class="token operator">:=</span> etcdv3<span class="token punctuation">.</span><span class="token function">NewRegistry</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	broker <span class="token operator">:=</span> kafka<span class="token punctuation">.</span><span class="token function">NewBroker</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	transport <span class="token operator">:=</span> nats<span class="token punctuation">.</span><span class="token function">NewTransport</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>
            micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            micro<span class="token punctuation">.</span><span class="token function">Registry</span><span class="token punctuation">(</span>registry<span class="token punctuation">)</span><span class="token punctuation">,</span>
            micro<span class="token punctuation">.</span><span class="token function">Broker</span><span class="token punctuation">(</span>broker<span class="token punctuation">)</span><span class="token punctuation">,</span>
            micro<span class="token punctuation">.</span><span class="token function">Transport</span><span class="token punctuation">(</span>transport<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span>

	service<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	service<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><h3 id="编写插件"><span>编写插件</span><a href="#编写插件" class="anchor">#</a></h3><p>插件是构建在 Go 接口之上的的概念。每个包都维护着高度抽象的接口。简单实现接口并把它作为选项传入服务。</p><p>服务发现的接口称作<a href="https://pkg.go.dev/github.com/micro/go-micro/v2/registry#Registry">注册（Registry）</a>。任何实现了这个接口的都可以当作注册中心。同样，对于其它包的实现也是如此。</p><pre class="language-go"><code><span class="token keyword">type</span> Registry <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token function">Register</span><span class="token punctuation">(</span><span class="token operator">*</span>Service<span class="token punctuation">,</span> <span class="token operator">...</span>RegisterOption<span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token function">Deregister</span><span class="token punctuation">(</span><span class="token operator">*</span>Service<span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token function">GetService</span><span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>Service<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token function">ListServices</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>Service<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token function">Watch</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>Watcher<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
<span class="token punctuation">}</span></code></pre><p>浏览<a href="https://github.com/micro/go-plugins">go-plugins</a>获取更多优秀实现内容。</p><h2 id="包装器（Wrappers）"><span>包装器（Wrappers）</span><a href="#包装器（Wrappers）" class="anchor">#</a></h2><p>Go-micro 中有中间件即包装器的概念。客户端或者处理器可以使用装饰模式包装起来。</p><h3 id="处理器"><span>处理器</span><a href="#处理器" class="anchor">#</a></h3><p>这里演示服务处理器包装器，它负责打印传入请求的日志。 Here&#x27;s an example service handler wrapper which logs the incoming request</p><pre class="language-go"><code><span class="token comment" spellcheck="true">// 实现server.HandlerWrapper接口</span>
<span class="token keyword">func</span> <span class="token function">logWrapper</span><span class="token punctuation">(</span>fn server<span class="token punctuation">.</span>HandlerFunc<span class="token punctuation">)</span> server<span class="token punctuation">.</span>HandlerFunc <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token keyword">func</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req server<span class="token punctuation">.</span>Request<span class="token punctuation">,</span> rsp <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"[%v] server request: %s"</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span><span class="token function">Endpoint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> <span class="token function">fn</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> req<span class="token punctuation">,</span> rsp<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>可以在创建服务时初始化</p><pre class="language-go"><code>service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>
	micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token comment" spellcheck="true">// 把handler包起来</span>
	micro<span class="token punctuation">.</span><span class="token function">WrapHandler</span><span class="token punctuation">(</span>logWrapper<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span></code></pre><h3 id="客户端"><span>客户端</span><a href="#客户端" class="anchor">#</a></h3><p>下面演示客户端包装器，它负责打印请求创建的日志。 Here&#x27;s an example of a client wrapper which logs requests made</p><pre class="language-go"><code><span class="token keyword">type</span> logWrapper <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	client<span class="token punctuation">.</span>Client
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>l <span class="token operator">*</span>logWrapper<span class="token punctuation">)</span> <span class="token function">Call</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req client<span class="token punctuation">.</span>Request<span class="token punctuation">,</span> rsp <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> opts <span class="token operator">...</span>client<span class="token punctuation">.</span>CallOption<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"[wrapper] client request to service: %s method: %s\n"</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span><span class="token function">Service</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span><span class="token function">Endpoint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> l<span class="token punctuation">.</span>Client<span class="token punctuation">.</span><span class="token function">Call</span><span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> req<span class="token punctuation">,</span> rsp<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// 实现client.Wrapper，充当日志包装器</span>
<span class="token keyword">func</span> <span class="token function">logWrap</span><span class="token punctuation">(</span>c client<span class="token punctuation">.</span>Client<span class="token punctuation">)</span> client<span class="token punctuation">.</span>Client <span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token operator">&amp;</span>logWrapper<span class="token punctuation">{</span>c<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>可以在创建服务时初始化</p><pre class="language-go"><code>service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>
	micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"greeter"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token comment" spellcheck="true">// 把客户端包起来</span>
	micro<span class="token punctuation">.</span><span class="token function">WrapClient</span><span class="token punctuation">(</span>logWrap<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span></code></pre><h2 id="相关示例"><span>相关示例</span><a href="#相关示例" class="anchor">#</a></h2><p>服务示例可以在<a href="https://github.com/micro/examples/tree/master/service"><strong>examples/service</strong></a>中找到，Function 则到<a href="https://github.com/micro/examples/tree/master/function"><strong>examples/function</strong></a>查看</p><p><a href="https://github.com/micro/examples"><strong>examples</strong></a>的 Github 目录下包含了各种示例，比如中间件/包装器，选择过滤器，发布/订阅，gRPC，插件等。</p><p>greeter 示例的完整代码<a href="https://github.com/micro/examples/tree/master/greeter"><strong>examples/greeter</strong></a>。</p><p>所有的示例都可以在 GitHub 仓库中找到。</p><p>观看<a href="https://www.youtube.com/watch?v=xspaDovwk34">Golang 英国会议 2016</a>视频，获得更高级的视角。</p></section><section class="markdown api-container"></section></article><div class="contributors-list" style="display:flex;list-style:none;margin:0;padding:0"><span style="opacity:0.3" class="ant-avatar ant-avatar-circle"><span class="ant-avatar-string" style="opacity:0"></span></span></div></section><section class="prev-next-nav"><a class="prev-page" href="/docs/micro/go-grpc-cn"><span role="img" aria-label="left" class="anticon anticon-left footer-nav-icon-before"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span>Go gRPC支持<span role="img" aria-label="right" class="anticon anticon-right footer-nav-icon-after"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></a><a class="next-page" href="/docs/micro/go-micro-internals-cn"><span role="img" aria-label="left" class="anticon anticon-left footer-nav-icon-before"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span>Go Micro 架构<span role="img" aria-label="right" class="anticon anticon-right footer-nav-icon-after"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></a></section><footer class="rc-footer rc-footer-dark"><section class="rc-footer-container"><section class="rc-footer-columns"><div class="rc-footer-column"><h2>相关资源</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/platform-web" target="_blank">Platform Web 治理平台</a></div><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/XConf" target="_blank">XConf 配置中心</a></div></div><div class="rc-footer-column"><h2>社区</h2><div class="rc-footer-item"><a href="https://github.com/websemantics/awesome-ant-design" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="comment" class="anticon anticon-comment"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="comment" width="1em" height="1em" fill="currentColor" aria-hidden="true"><defs><style></style></defs><path d="M573 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40zm-280 0c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path><path d="M894 345a343.92 343.92 0 00-189-130v.1c-17.1-19-36.4-36.5-58-52.1-163.7-119-393.5-82.7-513 81-96.3 133-92.2 311.9 6 439l.8 132.6c0 3.2.5 6.4 1.5 9.4a31.95 31.95 0 0040.1 20.9L309 806c33.5 11.9 68.1 18.7 102.5 20.6l-.5.4c89.1 64.9 205.9 84.4 313 49l127.1 41.4c3.2 1 6.5 1.6 9.9 1.6 17.7 0 32-14.3 32-32V753c88.1-119.6 90.4-284.9 1-408zM323 735l-12-5-99 31-1-104-8-9c-84.6-103.2-90.2-251.9-11-361 96.4-132.2 281.2-161.4 413-66 132.2 96.1 161.5 280.6 66 412-80.1 109.9-223.5 150.5-348 102zm505-17l-8 10 1 104-98-33-12 5c-56 20.8-115.7 22.5-171 7l-.2-.1A367.31 367.31 0 00729 676c76.4-105.3 88.8-237.6 44.4-350.4l.6.4c23 16.5 44.1 37.1 62 62 72.6 99.6 68.5 235.2-8 330z"></path><path d="M433 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path></svg></span></span>讨论</a></div><div class="rc-footer-item"><a href="/docs/community/join-us-cn#加入我们"><span class="rc-footer-item-icon"><span role="img" aria-label="usergroup-add" class="anticon anticon-usergroup-add"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="usergroup-add" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M892 772h-80v-80c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v80h-80c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h80v80c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8v-80h80c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8zM373.5 498.4c-.9-8.7-1.4-17.5-1.4-26.4 0-15.9 1.5-31.4 4.3-46.5.7-3.6-1.2-7.3-4.5-8.8-13.6-6.1-26.1-14.5-36.9-25.1a127.54 127.54 0 01-38.7-95.4c.9-32.1 13.8-62.6 36.3-85.6 24.7-25.3 57.9-39.1 93.2-38.7 31.9.3 62.7 12.6 86 34.4 7.9 7.4 14.7 15.6 20.4 24.4 2 3.1 5.9 4.4 9.3 3.2 17.6-6.1 36.2-10.4 55.3-12.4 5.6-.6 8.8-6.6 6.3-11.6-32.5-64.3-98.9-108.7-175.7-109.9-110.8-1.7-203.2 89.2-203.2 200 0 62.8 28.9 118.8 74.2 155.5-31.8 14.7-61.1 35-86.5 60.4-54.8 54.7-85.8 126.9-87.8 204a8 8 0 008 8.2h56.1c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5 29.4-29.4 65.4-49.8 104.7-59.7 3.8-1.1 6.4-4.8 5.9-8.8zM824 472c0-109.4-87.9-198.3-196.9-200C516.3 270.3 424 361.2 424 472c0 62.8 29 118.8 74.2 155.5a300.95 300.95 0 00-86.4 60.4C357 742.6 326 814.8 324 891.8a8 8 0 008 8.2h56c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5C505.8 695.7 563 672 624 672c110.4 0 200-89.5 200-200zm-109.5 90.5C690.3 586.7 658.2 600 624 600s-66.3-13.3-90.5-37.5a127.26 127.26 0 01-37.5-91.8c.3-32.8 13.4-64.5 36.3-88 24-24.6 56.1-38.3 90.4-38.7 33.9-.3 66.8 12.9 91 36.6 24.8 24.3 38.4 56.8 38.4 91.4-.1 34.2-13.4 66.3-37.6 90.5z"></path></svg></span></span>加入我们</a></div></div><div class="rc-footer-column"><h2>帮助</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/questions" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0138.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></span></span>GitHub</a></div></div><div class="rc-footer-column"><h2><span class="rc-footer-column-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/nBVXkrFdWHxbZlmMbsaH.svg" alt="Ant XTech"/></span>更多产品</h2><div class="rc-footer-item"><a href="https://yuque.com" target="_blank"><span class="rc-footer-item-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg" alt="yuque"/></span>语雀</a><span class="rc-footer-item-separator">-</span><span class="rc-footer-item-description">知识创作与分享工具</span></div></div></section></section><section class="rc-footer-bottom"><div class="rc-footer-bottom-container">Made with <span style="color:#fff">❤</span> by<!-- --> <a target="_blank" rel="noopener noreferrer" href="https://xtech.antfin.com">XTech</a></div></section></footer></div></div></div></div>
    </div>
    
    <script src="/index-c81a5111.js"></script>
    
    <!-- Global Site Tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-72788897-1"></script>
    <script>
      if (!location.port) {
        // Enable Google Analytics
        window.dataLayer = window.dataLayer || [];
        function gtag() {
          dataLayer.push(arguments);
        }
        gtag('js', new Date());
        gtag('config', 'UA-72788897-1');
      }
    </script>
    <!-- Hotjar Tracking Code for microhq.cn -->
    <script>
      (function (h, o, t, j, a, r) {
        h.hj =
          h.hj ||
          function () {
            (h.hj.q = h.hj.q || []).push(arguments);
          };
        h._hjSettings = { hjid: 1779539, hjsv: 6 };
        a = o.getElementsByTagName('head')[0];
        r = o.createElement('script');
        r.async = 1;
        r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
        a.appendChild(r);
      })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
    </script>
  </body>
</html>
